package cn.tedu.file;

import java.io.*;

/**本类用于联系字节输出流*/
public class TestOut {
    public static void main(String[] args) {
//        method1();//普通字节输出流
        method2();//高效字节输出流
    }

    private static void method2() {
        //1.定义一个在本方法中都生效的局部变量
        OutputStream out = null;
        //2.由于IO操作容易抛出异常，所有完成try-catch-finally结构
        try{
            //3.创建高效字节输出流对象 BufferedOutputStream

            //覆盖输出
//            out = new BufferedOutputStream(
//                    new FileOutputStream(new File("F://ready//1.txt")));
//            out = new BufferedOutputStream(
//                    new FileOutputStream("F:ready//1.txt"));


            //实现追加输出的效果
            out = new BufferedOutputStream(
                    new FileOutputStream(new File("F://ready//1.txt"),true));
            out = new BufferedOutputStream(
                    new FileOutputStream("F:\\ready\\1.txt",true));


            out.write(99);
            out.write(99);
            out.write(99);

        }catch(Exception e){
            e.printStackTrace();
        }finally {
            try {
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }

        }

    }

    private static void method1() {
        //1.创建字节输出流对象FOS-FileOutputString
        //1.1定义一在本方法中中都生效的局部变量
        OutputStream out = null;
        try {
//            out = new FileOutputStream(new File("F:\\ready\\1.txt"));//覆盖输出
//            out = new FileOutputStream("F:\\ready\\1.txt");//覆盖输出

            out = new FileOutputStream(new File("F:\\ready\\1.txt"),true);//追加输出
            out = new FileOutputStream("F:\\ready\\1.txt",true);//追加输出

            //2.使用流对象
            out.write(97);
            out.write(97);
            out.write(97);

        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            //3.关流
            try {
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }
}
